package puyu.manage.service.security.handle;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import com.alibaba.fastjson.JSON;
import puyu.manage.service.constant.AjaxResult;
import puyu.manage.service.constant.HttpStatus;
import puyu.manage.service.security.LoginUser;
import puyu.manage.service.security.service.TokenService;
import puyu.manage.service.utils.MessageUtils;
import puyu.manage.service.utils.ServletUtils;
import puyu.manage.service.utils.StringUtils;

import java.util.Map;

/**
 * 自定义退出处理类 返回成功
 *
 * @author cxnet
 */
@Configuration
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {
    @Autowired(required = false)
    private TokenService tokenService;

    /**
     * 退出处理
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser)) {
            String userName = loginUser.getUsername();
            // 删除用户缓存记录
            tokenService.delLoginUser(loginUser.getToken());

        }
        ServletUtils.renderString(response, JSON.toJSONString(
                AjaxResult.error(HttpStatus.SUCCESS, MessageUtils.message("A0010"))));
    }
}
